package com.example.android.marsphotos.network

import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
//import retrofit2.converter.scalars.ScalarsConverterFactory
import retrofit2.http.GET

private const val BASE_URL = "http://192.168.0.107:3000"

// 将 json 转为 kotlin 对象
private val moshi = Moshi.Builder()
    .add(KotlinJsonAdapterFactory())
    .build()

// 网络请求
private val retrofit = Retrofit.Builder()
    //.addConverterFactory(ScalarsConverterFactory.create())
    .addConverterFactory(MoshiConverterFactory.create(moshi))
    .baseUrl(BASE_URL)
    .build()

// 我们提供接口
interface MarsApiService {
    @GET("photos")
    suspend fun getPhotos(): List<MarsPhoto>
}

// 使用单例模式
object MarsApi {
    val retrofitService: MarsApiService by lazy {
        // 由 retrofit 来实现接口
        retrofit.create(MarsApiService::class.java)
    }
}